// 疑问的点 为什么要 toRaw
import { toRaw } from "@vue/runtime-core";
import { game } from "../Game";
import { enemyPlaneWidth, enemyPlaneHeight } from "./gameConfig";
// 碰撞检测
export const hitTestObject = (objA, objB) => {
	return (
		objA.x + objA.width >= objB.x &&
		objB.x + objB.width >= objA.x &&
		objA.y + objA.height >= objB.y &&
		objB.y + objB.height >= objA.y
	)
}
export const getRandomRange = (x, y) => {
	return Number(Math.round(Math.random() * (y - x)) + x);
}

export const parseConfig = (arr) => {
	let planeInfo = [];
	let planeIndex = 0;
	// console.log(arr);
	for (let i = 0; i < arr.length; i++) {
		const row = getRandomRange(arr[i][0], arr[i][1]);
		// console.log(row);
		for (let j = 1; j <= row; j++) {
			const y = -(Math.round(j * enemyPlaneHeight + 10 + (i / 3) * (game.screen.height - enemyPlaneHeight)));
			// console.log(y);
			// 往数组里插入对象，不能用 push ?
			planeInfo[planeIndex] = {
				x: getRandomRange(0, game.screen.width - enemyPlaneWidth),
				y: y,
				width: enemyPlaneWidth,
				height: enemyPlaneHeight,
				planeIndex
			}
			planeIndex++;
		}
	}
	return planeInfo;
}
// 如果把 round1 写成函数调用并返回值的形式 parseConfig([...]) ，会导致游戏不会更新，撞机重启还是原来初始化的数组
export const enemyPlaneConfig = [
	[
		[1, 3], [2, 3], [1, 4],
		[2, 4], [3, 4], [2, 4]
	],
	[
		// [1, 1]
		[2, 4], [3, 4], [2, 4],
		[3, 4], [3, 4], [3, 4]
	],
	[
		[3, 4], [3, 5], [3, 5],
		[4, 5], [4, 5], [4, 5]
	]
]

export const disAppearPlane = (enemyPlanes, planeIndex) => {
	for (let i = 0; i < enemyPlanes.length; i++) {
		if (enemyPlanes[i].planeIndex == planeIndex) {
			enemyPlanes.splice(i, 1)
			console.log(`${planeIndex}已消失`);
			break;
		}
	}
	return enemyPlanes
}
